//
//  HTTPManager.m
//  dongpeng-iOs
//
//  Created by 张育铭 on 2017/9/21.
//  Copyright © 2017年 dongpeng. All rights reserved.
//

#import "HTTPManager.h"
#import <SVProgressHUD/SVProgressHUD.h>

@implementation HTTPManager

static HTTPManager *httpManager = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        httpManager = [[HTTPManager alloc]initWithBaseURL:[NSURL URLWithString:@"https://dev.guohaozhongyi.com"]];
        httpManager.requestSerializer.timeoutInterval = 12.0f;
        httpManager.requestSerializer = [AFJSONRequestSerializer serializer];
        // 设置请求头
        [httpManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [httpManager.requestSerializer setValue:@"ba1f2511fc30423bdbb183fe33f3dd0f" forHTTPHeaderField:@"GH-TOKEN"];
        [httpManager.requestSerializer setValue:@"15727" forHTTPHeaderField:@"GH-UID"];
        
    });
    return httpManager;
}


- (NSURLSessionDataTask*)postWithPath:(NSString*)path parametersDict:(NSDictionary*)Parameter sucessBlock:( void(^)(NSURLSessionDataTask *operation, id responseObject) )sucess failtBlock:( void(^)(NSURLSessionDataTask *operation, id error) )failt
{
    NSLog(@"%@  \n  %@",path,Parameter);
    return [httpManager POST:path parameters:Parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (sucess) {
            sucess(task,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failt) {
            failt(task,error);
        }
    }];
}

- (NSURLSessionDataTask*)GetWithPath:(NSString*)path parametersDict:(NSDictionary*)Parameter sucessBlock:( void(^)(NSURLSessionDataTask *operation, id responseObject) )sucess failtBlock:( void(^)(NSURLSessionDataTask *operation, id error) )failt {
    NSLog(@"%@  \n  %@",path,Parameter);
    return [httpManager GET:path parameters:Parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (sucess) {
            sucess(task,responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failt) {
            failt(task,error);
        }
    }];
}









- (NSURLSessionDataTask *)postWithPath:(NSString *)path
                        parametersDict:(NSDictionary *)Parameter
                             imageData:(NSData *)imageData
                              postName:(NSString *)postName
                              fileName:(NSString *)fileName
                              miniType:(NSString *)miniType
                          successBlock:(void (^)(NSURLSessionDataTask *operation, id responseObject))success
                            failtBlock:(void (^)(NSURLSessionDataTask *operation, NSError *error))failt
{
    return [httpManager POST:path parameters:Parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:postName fileName:fileName mimeType:miniType];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        NSLog(@"%@",error.userInfo);
        NSString *errorStr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
        NSLog(@"执行失败 %@", errorStr);
        if (error.code  == -1001) {
            [SVProgressHUD showErrorWithStatus:@"网络超时,请稍后再试!"];
        }
    }];
}

- (NSURLSessionDataTask*)postWithPath:(NSString*)path parametersDict:(NSDictionary*)Parameter sucessBlock:( void(^)(id responseObject))sucess
{
    NSLog(@"%@  \n  %@",path,Parameter);
    return [httpManager POST:path parameters:Parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [SVProgressHUD dismiss];
        sucess(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.userInfo);
        NSString *errorStr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
        NSLog(@"执行失败 %@", errorStr);
        if (error.code  == -1001) {
            [SVProgressHUD showErrorWithStatus:@"网络超时,请稍后再试!"];
        }
    }];
}



@end
